#include <assert.h>
#include "remez.h"
#include "remez_exp.h"

using namespace mpfr;

// 计算exp(x)
mpreal exp_func(mpreal x)
{
    return exp(x);
}

// evaluatePolynomial(x)  = coeffs[0] + coeffs[1]*x + coeffs[2]*x^2 + coeffs[3]*x^3 + coeffs[4]*x^4 + coeffs[5]*x^5
// evaluatePolynomial(x) -exp(x)的导数， f′(x)- exp′(x)
mpreal exp_error_derivative(const std::vector<mpreal> &coeffs, mpreal x0,  int degree) // degree为多项式的阶数
{
     assert(degree+1<=coeffs.size());

    // coeffs=[c0,c1,c2,c3,c4,c5]
    // f(x)= c0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4 + c5*x^5
    // f′(x)=c1 + 2*c2*x + 3*c3*x^2 + 4*c4*x^3+ 5*c5*x^4
    mpreal r= polynomial_derivative( coeffs, x0, degree);
    
    return r-exp_func(x0);
}

// 计算my_exp(x)-exp_func(x),即计算　myexp　在x点的误差
mpreal exp_get_error_at_x( const std::vector<mpreal> &coeffs, mpreal x, int degree) // degree为多项式的阶数
{
    mpreal r = evaluatePolynomial(coeffs,x,degree);
    return r-exp_func(x);
}
